package com.hs.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.HeartMark;
import com.hs.sfzy.entity.HeartTest;
import com.hs.sfzy.mapper.HeartMarkMapper;
import com.hs.sfzy.mapper.HeartTestMapper;
import com.hs.sfzy.mapper.MyTestMapper;
import com.hs.sfzy.service.HeartMarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName HeartMarkServiceImpl
 * @Description TODO
 * @Author QinKunPeng
 * @Date 2024年10月24日 10:56
 * @Version 1.0
 */
@Service
@Transactional
public class HeartMarkServiceImpl implements HeartMarkService {

    @Autowired
    HeartMarkMapper heartMarkMapper;

    @Autowired
    HeartTestMapper heartTestMapper;

    @Autowired
    MyTestMapper myTestMapper;


    @Override
    public List<HeartMark> getAllHeartMark(HeartMark heartMark) {
        List<HeartMark> allHeartMark = heartMarkMapper.getAllHeartMark(heartMark);
        allHeartMark.forEach(heartMark1 -> {
            heartMark1.setTotal(heartMark1.getHeartTestList().size());
        });
        return allHeartMark;
    }

    @Override
    public PageInfo<HeartMark> getHeartMarkByPage(int pageNum, int pageSize, HeartMark heartMark) {
        PageHelper.startPage(pageNum, pageSize);
        List<HeartMark> allHeartMark = heartMarkMapper.getAllHeartMark(heartMark);
        allHeartMark.forEach(heartMark1 -> {
            heartMark1.setTotal(heartMark1.getHeartTestList().size());
        });
        PageInfo<HeartMark> pageInfo = new PageInfo<HeartMark>(allHeartMark);
        return pageInfo;
    }

    @Override
    public HeartMark getHeartMarkById(long id) {
        return heartMarkMapper.getHeartMarkById(id);
    }

    @Override
    public int updateHeartMark(HeartMark heartMark) {
        return heartMarkMapper.updateHeartMark(heartMark);
    }

    @Override
    public int insertHeartMark(HeartMark heartMark) {
        heartMark.setCreateTime(new Date());
        int num = heartMarkMapper.insertHeartMark(heartMark);
        List<HeartTest> heartTestList = heartMark.getHeartTestList();
        if (num > 0){
            int lastIndex = heartMarkMapper.getLastIndex();
            if (heartTestList != null && heartTestList.size() > 0) {
                for (HeartTest heartTest : heartTestList) {
                    heartTest.setHeartId(lastIndex);
                    heartTestMapper.insert(heartTest);
                }
            }
            return num;
        }
        return num;
    }

    @Override
    public int deleteHeartMark(long id) {
        heartTestMapper.deleteByHeartId((int) id);
        myTestMapper.deleteByHeartMarkId(id);
        return heartMarkMapper.deleteHeartMark(id);
    }

    @Override
    public List<Integer> getHeartMarkIds(String name) {
        return heartMarkMapper.getHeartMarkId(name);
    }

    @Override
    public List<HeartMark> findAnyInfo() {
        return heartMarkMapper.findAnyInfo();
    }
}
